package com.greensecurity.sim.response;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.greensecurity.sim.shiro.ResponseObject;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import springfox.documentation.spring.web.json.Json;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.UiConfiguration;

import java.util.ArrayList;

@RestControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        ResponseObject responseObject = new ResponseObject();
        if (o instanceof Json || o instanceof UiConfiguration || (o instanceof ArrayList && ((ArrayList) o).get(0) instanceof SwaggerResource))
            return o;
        if(o instanceof String){
            return objectMapper.writeValueAsString(responseObject.success(o));
        }
        if(o instanceof ResponseObject){
            return o;
        }
        return responseObject.success(o);
    }
}
